#!/usr/local/bin/python
"""cgi to print selected METAR reports from database.
"""

__RCS__ = '$Id$'
__version__ = '$Revision:$'
__initialdate__ = 'November 2007'
__license__ = '''Copyright (c) 2007, Darren Paul Griffith, All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.'''

__author__ = 'Darren Paul Griffith <http://madphilosopher.ca/>'

import sqlite3
import sys

conn = sqlite3.connect('wg3.db')
c = conn.cursor()

def getStationIDs():
    '''Returns a list of Station IDs from the database.'''

    dict = {}
    c.execute('select station_id from metar where station_id is not null;')

    for row in c:
        id = row[0].strip()
        if not dict.has_key(id): dict[id] = 0
    stations = dict.keys()
    stations.sort()
    return stations


def printStationRadioButtons():
    '''Prints the HTML for Radio Buttons of all the stations'''

    stations = getStationIDs()
    TEMPLATE = """<INPUT TYPE="radio" NAME="stations" VALUE="%s" %s > %s &nbsp;&nbsp;&nbsp;"""
    CHECKED = "CHECKED"
    station_id = ""

    print """<FORM ACTION="report.cgi" METHOD="POST"> """
    if len(stations) == 0:
        print "Error: station list should not be empty"
        sys.exit(1)
    else:
        count = 0
        for station_id in stations:
            if count <> 0:
                CHECKED = ""
            print TEMPLATE % (station_id, CHECKED, station_id)
            count = count + 1
        
    print """<INPUT TYPE="submit" VALUE="Submit">"""
    print """<INPUT TYPE="reset"  VALUE="Clear">"""
    print """</FORM>"""
    print """<hr>"""


def printStationReport(station_id):
    '''Prints the METAR reports for the given station_id.'''

    t = (station_id,)
    c.execute('select timestamp,code from metar where station_id =?;', t)

    for row in c:
        timestamp, code = row
        print timestamp, code



if __name__ == '__main__':


    printStationRadioButtons()

    station_id = u"CYXD"
    printStationReport(station_id)

